﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace EvoBoost.Core
{
	public static class Statistics
	{
		public static double CalculateMean(IEnumerable<double> distribution)
		{
			if (distribution == null) throw new ArgumentNullException("distribution");

			return distribution.Average();
		}

		public static double CalculateVariance(IEnumerable<double> distribution)
		{
			if (distribution == null) throw new ArgumentNullException("distribution");

			double mean = CalculateMean(distribution);
			return CalculateVariance(mean, distribution);
		}

		public static double CalculateVariance(double mean, IEnumerable<double> distribution)
		{
			if (distribution == null) throw new ArgumentNullException("distribution");

			return distribution.Sum(d => (d - mean) * (d - mean)) / (distribution.Count() - 1.0);
		}

		public static double CalculateStdDev(IEnumerable<double> distribution)
		{
			if (distribution == null) throw new ArgumentNullException("distribution");

			double variance = CalculateVariance(distribution);
			return Math.Sqrt(variance);
		}
	}
}
